<?php 
header('content-type:text/html;charset=utf8');
$username=$_POST['username'];
$password=$_POST['password'];
$con=mysqli_connect('localhost','root','root','xiaomi');
mysqli_query($con,'set names utf8');
//查询账号是否被占用
$res = mysqli_query($con,"select * from user where username='$username'");
$row = mysqli_fetch_assoc($res);
if($row){
    //提取到数据了就表示账号被占用
    $arr=[
        "meta"=>[
            "status"=>2,
            "msg"=>"账号被占用"
        ],
        "data"=>null
    ];
}else{
    //没有占用 -可以注册
    // 将数据添加到数据库
    $res=mysqli_query($con,"insert user(username,password) values('$username','$password')");
    if($res){//添加成功
        $arr=[
            "meta"=>[
                "status"=>0,
                "msg"=>"注册成功"
            ],
            "data"=>null
        ];
    }else{
        $arr=[
            "meta"=>[
                "status"=>1,
                "msg"=>"注册失败"
            ],
            "data"=>null
            ];
    }
}
echo json_encode($arr);
?>